import Vue from "vue";
const state = {
  dataList: [], // 已添加部分的渲染数据
  drawerFlag: true, // 控制右侧面板是否弹出
  allGraphData: {}, // 用来保存当前图中所有的节点
  allNodesId: [], // 用来保存当前图中所有的节点 id
  graphDatas: {}, // 所有的图节点和边数据
  drawerTypeOfNode: "empty",
  drawerTypeOfEdge: "empty",
  neo4jIdOfNode: null,
  neo4jIdOfEdge: null,
  ontologyId: null,
  ontologyAllId: "", // 当前选中的本体项 id
  ontologyAllName: "", //当前选中的本体项 name
  newEdgeOfG6: {
    edgeId: null, // g6Id,
    sourceId: null, // entityId,
    targetId: null,
    sourceOntoId: null, // ontologyId,
    targetOntoId: null,
  },
  currentNode: {},
  currentEdge: {},
  node_config_data: [],
  edge_config_data: [],
  edge_config_data_ids: [],
  nodeConfigItem: {
    id: -1,
    color: "#7E9BFA",
    radius: 16,
    label: "所选节点",
  },
  edgeConfigItem: {
    id: -1,
    label: "所选连边",
    color: "#A9B6C3",
    edgeWidth: 2,
    isArrow: true,
  },
  isVisual: false,
  storeNodes: [],
  storeEdges: [],
};

const mutations = {
  change_ontologyAllId(state, payload) {
    state.ontologyModelId = payload;
  },
  // 向已添加中添加
  add_dataList(state, payload) {
    state.dataList.push(payload);
  },
  // 从已添加中根据 id 删除
  delete_dataList_byId(state, payload) {
    state.dataList = state.dataList.filter((item) => {
      if (item.id !== payload) return item;
    });
  },
  change_drawerFlag(state, payload) {
    state.drawerFlag = !state.drawerFlag;
  },
  change_drawer_type_of_node(state, payload) {
    state.drawerTypeOfNode = payload;
  },
  change_drawer_type_of_edge(state, payload) {
    state.drawerTypeOfEdge = payload;
  },
  save_nodesId(state, payload) {
    state.allNodesId = payload;
  },
  change_current_node(state, payload) {
    if (!payload) return;

    state.currentNode = payload;
    if (state.isVisual) {
      /*  const currentNode_state = state.node_config_data.find((node) => {
        return (
          node.id === payload.ontology_id ||
          (payload.is_private && node.isPrivate)
        );
      }); */
      state.node_config_data[0].color = payload.style.fill;
      state.node_config_data[0].radius = payload.size / 2;
      return;
    }
    const currentNode_state = state.node_config_data.find((node) => {
      return node.id === payload.id;
    });
    if (!currentNode_state) return;
    state.node_config_data[0].color = currentNode_state.color;
    state.node_config_data[0].radius = currentNode_state.radius;
  },
  change_current_edge(state, payload) {
    if (!payload) return;
    state.currentEdge = payload;

    if (state.isVisual) {
      /* const currentEdge_state = state.edge_config_data.find((edge) => {
        return (
          edge.relationId === payload.relationId ||
          (payload.is_private && edge.isPrivate)
        );
      });
 */
      state.edge_config_data[0].color = payload.style.stroke;
      state.edge_config_data[0].edgeWidth = payload.style.lineWidth;
      state.edge_config_data[0].isArrow = payload.style.endArrow.path !== "";
      return;
    }

    const currentEdge_state = state.edge_config_data.find((edge) => {

      return edge.id === payload.id;
    });
    if (!currentEdge_state) return;
    state.edge_config_data[0].color = currentEdge_state.color;
    state.edge_config_data[0].edgeWidth = currentEdge_state.edgeWidth;
    state.edge_config_data[0].isArrow = currentEdge_state.isArrow;
  },
  change_neo4jId_of_node(state, payload) {
    state.neo4jIdOfNode = payload;
  },
  change_neo4jId_of_edge(state, payload) {
    state.neo4jIdOfEdge = payload;
  },
  change_ontologyId(state, payload) {
    state.ontologyId = payload;
  },
  create_new_edge_of_g6(state, payload) {
    state.newEdgeOfG6 = Object.assign(state.newEdgeOfG6, payload);
  },
  save_graphData(state, payload) {
    state.graphDatas = payload;
  },
  add_node(state, payload) {
    if (state.graphDatas.ontologyNodes) {
      state.graphDatas.ontologyNodes.push(payload);
    } else {
      state.graphDatas.ontologyNodes = [payload];
    }
  },
  remove_node(state, payload) {
    if (state.graphDatas.ontologyNodes) {
      let delIndex = state.graphDatas.ontologyNodes.findIndex((item) => {
        return item.id === payload.id;
      });
      if (delIndex > -1) {
        state.graphDatas.ontologyNodes.splice(delIndex, 1);
      }
    }
  },
  update_node(state, payload) {
    if (state.graphDatas.ontologyNodes) {
      let updateIndex = state.graphDatas.ontologyNodes.findIndex((item) => {
        return item.id === payload.id;
      });
      if (updateIndex > -1) {
        state.graphDatas.ontologyNodes.splice(updateIndex, 1, payload);
      }
    }
  },
  add_edge(state, payload) {
    if (state.graphDatas.ontologyEdges) {
      state.graphDatas.ontologyEdges.push(payload);
    } else {
      state.graphDatas.ontologyEdges = [payload];
    }
  },
  remove_edge(state, delId) {
    if (state.graphDatas.ontologyEdges) {
      let delIndex = state.graphDatas.ontologyEdges.findIndex((item) => {
        return item.id === delId;
      });
      if (delIndex > -1) {
        state.graphDatas.ontologyEdges.splice(delIndex, 1);
      }
    }
  },
  update_edge(state, payload) {
    if (state.graphDatas.ontologyEdges) {
      let updateIndex = state.graphDatas.ontologyEdges.findIndex((item) => {
        return item.id === payload.id;
      });
      if (updateIndex > -1) {
        state.graphDatas.ontologyEdges.splice(updateIndex, 1, payload);
      }
    }
  },
  save_ontologyAllId(state, payload) {
    state.ontologyAllId = payload;
  },
  save_ontologyAllName(state, payload) {
    state.ontologyAllName = payload;
  },
  save_currentNodeId(state, payload) {
    state.currentNodeId = payload.neo4j;
    state.ontologyId = payload.mysql;
  },
  create_node_config_data(state, payload) {

    state.node_config_data = payload;
    state.node_config_data.forEach((node) => {
      Vue.set(node, "color", node.color);
      Vue.set(node, "radius", 16);
      Vue.set(node, "selected", true);
      // node.radius = 30;
      // node.selected = true;
    });

    state.node_config_data.unshift(state.nodeConfigItem);
  },
  create_edge_config_data(state, payload) {
    if (Array.isArray(payload)) {
      state.edge_config_data = payload;
      state.edge_config_data.forEach((edge) => {
        Vue.set(edge, "color", edge.color);
        Vue.set(edge, "edgeWidth", 2);
        Vue.set(edge, "isArrow", true);
        Vue.set(edge, "selected", true);
        // Vue.set(edge, "isPrivate", "0");
        // edge.edgeWidth = 1;
        // edge.isArrow = true;
        // edge.selected = true;
      });
      state.edge_config_data.unshift(state.edgeConfigItem);
    } else {
      // const { res, result } = payload;
      // state.edge_config_data_ids = res;
      // state.edge_config_data = result;
      // state.edge_config_data.forEach((edge) => {
      //   Vue.set(edge, "color", edge.color);
      //   Vue.set(edge, "edgeWidth", 2);
      //   Vue.set(edge, "isArrow", true);
      //   Vue.set(edge, "selected", true);
      //   // Vue.set(edge, "isPrivate", "1");
      //   // edge.edgeWidth = 1;
      //   // edge.isArrow = true;
      //   // edge.selected = true;
      // });
      // state.edge_config_data.unshift(state.edgeConfigItem);
    }
  },
  change_node_config_data(state, payload) {
    let { nodes, color, radius } = payload;
    if (typeof nodes !== "object") {
      throw Error("请传数组或者对象类型参数");
    }
    let changeTargets = nodes;
    if (!Array.isArray(nodes)) {
      changeTargets = [nodes];
    }
    changeTargets.forEach((item) => {
      let currentNode = state.node_config_data.find((node) => {
        return node.id === item.id;
      });
      if (currentNode) {
        let _color = color ? color : currentNode.color;
        let _radius = radius ? radius : currentNode.radius;

        Vue.set(currentNode, "color", _color);
        Vue.set(currentNode, "radius", _radius);
      }
    });
  },
  change_edge_config_data(state, payload) {
    let { edges, color, edgeWidth, isArrow, type } = payload;
    if (typeof edges !== "object") {
      throw Error("请传数组或者对象类型参数");
    }
    let changeTargets = edges;
    if (!Array.isArray(edges)) {
      changeTargets = [edges];
    }
    console.log("currentEdge", edges);
    changeTargets.forEach((item) => {
      let currentEdge = state.edge_config_data.find((edge) => {
        return (
          edge.id === item.id ||
          (state.isVisual && edge.isPrivate && item.is_private) ||
          (state.isVisual && edge.relationId && item.relationId)
        );
      });
      if (currentEdge) {
        let _color = color ? color : currentEdge.color;
        let _edgeWidth = edgeWidth ? edgeWidth : currentEdge.edgeWidth;
        let _isArrow = type === "isArrow" ? isArrow : currentEdge.isArrow;
        Vue.set(currentEdge, "color", _color);
        Vue.set(currentEdge, "edgeWidth", _edgeWidth);
        Vue.set(currentEdge, "isArrow", _isArrow);
      }
    });
  },
  change_default_node_color(state, color) {
    state.node_config_data[0].color = color;
  },
  change_default_node_radius(state, radius) {
    state.node_config_data[0].radius = radius;
  },
  change_default_edge_color(state, color) {
    state.edge_config_data[0].color = color;
  },
  change_default_edge_edgeWidth(state, edgeWidth) {
    state.edge_config_data[0].edgeWidth = edgeWidth;
  },
  change_default_edge_arrow(state, isArrow) {
    console.log("isArrow", isArrow);

    state.edge_config_data[0].isArrow = isArrow;
  },
  remove_node_config_data(state, id) {
    let currentNode = state.node_config_data;
    let removeIndex = currentNode.findIndex((node) => node.id === id);
    currentNode.splice(removeIndex, 1);
  },
  remove_edge_config_data(state, id) {
    let currentEdge = state.edge_config_data;
    let removeIndex = currentEdge.findIndex((edge) => edge.id === id);
    currentEdge.splice(removeIndex, 1);
  },
  change_isVisual(state, isVisual) {
    state.isVisual = isVisual;
  },
  change_storeNodes(state, storeNodes) {

    if (Array.isArray(storeNodes)) {
      state.storeNodes = storeNodes;
    } else {
      if (!state.storeNodes.some((node) => node.id == storeNodes.id)) {
        return state.storeNodes.push(storeNodes);
      }
      state.storeNodes.forEach((node, index) => {
        if (node.id == storeNodes.id) {
          Vue.set(state.storeNodes, index, storeNodes);
        }
      });
    }
  },
  change_storeEdges(state, storeEdges) {
    if (Array.isArray(storeEdges)) {
      state.storeEdges = storeEdges;
    } else {
      if (!state.storeEdges.some((edge) => edge.id == storeEdges.id)) {
        return state.storeEdges.push(storeEdges);
      }
      state.storeEdges.forEach((edge, index) => {
        if (edge.id == storeEdges.id) {
          Vue.set(state.storeEdges, index, storeEdges);
        }
      });
    }
  },
};

export default {
  state,
  mutations,
};
